/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package etherblocks.mods.survival.entitysystem.systems;

import java.util.List;
import com.jme3.math.Vector3f;
import etherblocks.game.entitysystem.*;
import etherblocks.game.entitysystem.components.*;
import etherblocks.mods.survival.entitysystem.components.*;

/**
 *
 * @author Philipp
 */
public class StareSystem implements EntitySystem
{

    public void onNextFrameCalculation(EntityMap entityMap, float lastTimePerFrame) {
        List<Entity> entities = entityMap.getEntities(HasTargetComponent.class, LocationComponent.class);
        for(Entity entity : entities)
        {
            Entity target = new Entity(entityMap, entity.getComponent(HasTargetComponent.class).getTargetID());
            LocationComponent locationComponent = target.getComponent(LocationComponent.class);
            Stare(entity, locationComponent.getLocation());
        }
    }
    
    private void Stare(Entity entity, Vector3f target)
    {
        LocationComponent locationComponent = entity.getComponent(LocationComponent.class);
        Vector3f direction = target.subtract(locationComponent.getLocation()).normalizeLocal();
        entity.setComponent(new RotationComponent(direction, System.currentTimeMillis()));
        entity.setComponent(new MoveDirectionComponent(direction.mult(0.06f)));
    }
}
